package com.jzo2o.foundations.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeAggregationSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeAggregationTypeSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {

    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO servePageQueryReqDTO) {
        //通过baseMapper调用queryServeListByRegionId方法
        PageResult<ServeResDTO> serveResDTOPageResult = PageHelperUtils.selectPage(servePageQueryReqDTO,
                () -> baseMapper.queryServeListByRegionId(servePageQueryReqDTO.getRegionId()));
        return serveResDTOPageResult;

    }
    @Resource
    private ServeItemMapper serveItemMapper;
    @Resource
    private RegionMapper regionMapper;
    @Override
    @Transactional
    public void batchAdd(List<ServeUpsertReqDTO> serveUpsertReqDTOList) {
        for (ServeUpsertReqDTO serveUpsertReqDTO : serveUpsertReqDTOList) {
            ServeItem serveItem = serveItemMapper.selectById(serveUpsertReqDTO.getServeItemId());
            if(ObjectUtil.isEmpty(serveItem)||serveItem.getActiveStatus()== FoundationStatusEnum.DISABLE.getStatus()){
                throw new ForbiddenOperationException("该服务未启用无法添加到区域下使用");
            }
            Integer count = lambdaQuery()
                    .eq(Serve::getRegionId, serveUpsertReqDTO.getRegionId())
                    .eq(Serve::getServeItemId, serveUpsertReqDTO.getServeItemId())
                    .count();
            if(count>0){
                throw new ForbiddenOperationException(serveItem.getName() + "服务已存在");
            }
            Serve serve = BeanUtil.toBean(serveUpsertReqDTO, Serve.class);
            Region region = regionMapper.selectById(serveUpsertReqDTO.getRegionId());
            serve.setCityCode(region.getCityCode());
            baseMapper.insert(serve);
        }
    }
    /**
     * 删除区域服务
     * @param id
     */
    @Override
    public void delete(Long id) {
        Serve serve = baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(serve)){
            throw new ForbiddenOperationException("该区域服务不存在");
        }
        if(serve.getSaleStatus()!= FoundationStatusEnum.INIT.getStatus()){
            throw new ForbiddenOperationException("草稿状态才能删除");
        }
        int i = baseMapper.deleteById(id);
        if(i!=1){
            throw new ForbiddenOperationException("区域服务删除失败");
        }

    }
    /**
     * 区域服务下架
     * @param id
     */
    @Override
    public void offSale(Long id) {
        Serve serve = baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(serve)){
            throw new ForbiddenOperationException("该区域服务不存在");
        }
        if(serve.getSaleStatus()!=FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("启用状态才能下架");
        }
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus, FoundationStatusEnum.DISABLE.getStatus())
                .update();
        if(!update){
            throw new ForbiddenOperationException("区域服务下架失败");
        }

    }
    /**
     * 区域服务上架
     * @param id
     */
    @Override
    public void onSale(Long id) {
        Serve serve = baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(serve)){
            throw new ForbiddenOperationException("该区域服务不存在");
        }
        if(serve.getSaleStatus()!=FoundationStatusEnum.DISABLE.getStatus()){
            throw new ForbiddenOperationException("禁用状态才能上架");
        }
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus())
                .update();
        if(!update){
            throw new ForbiddenOperationException("区域服务上架失败");
        }
    }

    /**
     * 设置热门
     * @param id
     */
    @Override
    public void onHot(Long id) {
        Serve serve = baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(serve)){
            throw new ForbiddenOperationException("该区域服务不存在");
        }
        if(serve.getIsHot()==1){
            throw new ForbiddenOperationException("非热门状态才能设置热门");
        }
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getIsHot, 1)
                .update();
        if(!update){
            throw new ForbiddenOperationException("区域服务设置热门失败");
        }
    }
    /**
     * 取消热门
     * @param id
     */
    @Override
    public void offHot(Long id) {
        Serve serve = baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(serve)){
            throw new ForbiddenOperationException("该区域服务不存在");
        }
        if(serve.getIsHot()==0){
            throw new ForbiddenOperationException("热门状态才能取消热门");
        }
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getIsHot, 0)
                .update();
        if(!update){
            throw new ForbiddenOperationException("区域服务取消热门失败");
        }
    }

    @Override
    public Serve updatePrice(Long id, BigDecimal price) {
        boolean update = lambdaUpdate()
                .eq(Serve::getId,id)
                .set(Serve::getPrice,price)
                .update();
        if(!update){
            throw new ForbiddenOperationException("修改服务价格失败");
        }
        return baseMapper.selectById(id);

    }

    @Override
    public List<ServeAggregationSimpleResDTO> findHotServeListByRegionId(Long regionId) {
        return baseMapper.findHotServeListByRegionId(regionId);
    }

    @Override
    public List<ServeAggregationTypeSimpleResDTO> findServeTypeListByRegionId(Long regionId) {
        return baseMapper.findServeTypeListByRegionId(regionId);
    }


}
